package com.zhouyibike.uu.ui.activity.mywallet;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.YCQResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.listener.MyListener;
import com.zhouyibike.uu.ui.adapter.YCQAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyYHQActivity extends BaseActivity implements View.OnClickListener{
    private ImageButton mBtnBack;
    private TextView mBtnTitle;

    private LinearLayout ll_nothing;
    private PullToRefreshLayout pullToRefreshLayout;
    private ListView lv_allmsg;
    private int  refreshtype=0;
    private int page=1;//页码
    private boolean ismore=false;//判断是否还有更多数据
    private YCQAdapter ycqAdapter;
    private List<YCQResult.DataBean> alllist=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_yhq);
        initView();
        getMsg();
    }
    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("骑行券");
        mBtnBack.setOnClickListener(this);

        pullToRefreshLayout=((PullToRefreshLayout) findViewById(R.id.refresh_view));
        pullToRefreshLayout.setOnRefreshListener(new MyListener2());
        ll_nothing= (LinearLayout) findViewById(R.id.ll_nothing_show);
        lv_allmsg=(ListView) findViewById(R.id.content_view);
    }
    private void initData(){
        if(page==1) {
            ycqAdapter = new YCQAdapter(this, alllist);
            lv_allmsg.setAdapter(ycqAdapter);
        }else{
            //刷新数据
            ycqAdapter.notifyDataSetChanged();
        }
    }
    class MyListener2 extends MyListener {
        @Override
        public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
            super.onRefresh(pullToRefreshLayout);
            refreshtype=1;
            page=1;
            ismore=false;
            getMsg();
        }

        @Override
        public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            super.onLoadMore(pullToRefreshLayout);
            refreshtype=2;
            if(ismore) {
                page++;
                getMsg();
            }else{
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }
    }
    private void getMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        if(page==1&&refreshtype==0)
             showProgressDialog();
        addSubscription(apiStores.GetPackages(params),
                new ApiCallback<YCQResult>() {

                    @Override
                    public void onSuccess(YCQResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            if( refreshtype==1){
                                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                                alllist.clear();
                            }else if( refreshtype==2){
                                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                            }
                            if(model.getData().size()==0){
                                ismore=false;
                            }
                            alllist.addAll(model.getData());
                            if(alllist.size()==0){
                                pullToRefreshLayout.setVisibility(View.GONE);
                                ll_nothing.setVisibility(View.VISIBLE);
                            }else{
                                pullToRefreshLayout.setVisibility(View.VISIBLE);
                                ll_nothing.setVisibility(View.GONE);
                                initData();
                            }
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
        }
    }
}
